//
//  WCNCaseVC.m
//  WCN
//
//  Created by 杨鑫 on 15/6/11.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNCaseVC.h"
#import "WCNCaseDetailVC.h"
//
#import "MJRefresh.h"
//
#import "WCNConstantsCDStore.h"
#import "WCPopupLayerManager.h"
#import "WCNetworkAccessManager.h"
//
#import "Macros.h"
#import "Constants.h"

static NSString *cellId = @"UITableViewCell";

@interface WCNCaseVC ()

@property (strong, nonatomic) NSArray *careCases;
@property (strong, nonatomic) NSString *url;

@end

@implementation WCNCaseVC

- (instancetype)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}

- (instancetype)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        self.title = @"护理案例";
        
        _careCases = [[WCNConstantsCDStore shareStore] getCareCases];
        _url = @"template/nursing/case/list";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.navigationItem.leftBarButtonItems = @[];
    self.view.backgroundColor = kAppBg;
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellId];
    
    __weak typeof(self) weakSelf = self;
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf refresh];
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if ([[WCNetworkAccessManager sharedMg] timeSinceLastUrl:self.url] > 2*kUrlCD) {
        
//        UIView *titleView = self.navigationItem.titleView;
//        self.navigationItem.titleView = [self titleViewForActivity];
        
        [[WCNetworkAccessManager sharedMg] GET:self.url parameters:nil success:^(NSDictionary *result){
            self.careCases = [[WCNConstantsCDStore shareStore] createCareCases:result[@"data"]];
            [self.tableView reloadData];
        } failure:nil netError:nil always:^{
//            self.navigationItem.titleView = titleView;
        }];
    }
}

//- (UIView *)titleViewForActivity
//{
//    UILabel *titleLabel = [[UILabel alloc] init];
//    titleLabel.text = @"护理案例";
//    titleLabel.font = [UIFont boldSystemFontOfSize:20];
//    CGSize size = [titleLabel systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
//    titleLabel.frame = CGRectMake((160-size.width)/2.0, (30-size.height)/2.0, size.width, size.height);
//    
//    UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
//    activityView.frame = CGRectMake((160-size.width)/2.0-20, 5, 20, 20);
//    [activityView startAnimating];
//    
//    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 30)];
//    [titleView addSubview:titleLabel];
//    [titleView addSubview:activityView];
//    return titleView;
//}

- (void)refresh
{
    [[WCNetworkAccessManager sharedMg] GET:self.url parameters:nil success:^(NSDictionary *result){
        self.careCases = [[WCNConstantsCDStore shareStore] createCareCases:result[@"data"]];
        [self.tableView reloadData];
    } failure:nil netError:nil always:^{
        [self.tableView.header endRefreshing];
    }];
}

#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.careCases.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    CareCase *careCase = self.careCases[indexPath.row];
    
    cell.textLabel.text = careCase.name;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CareCase *careCase = self.careCases[indexPath.row];
    WCNCaseDetailVC *vc = [[WCNCaseDetailVC alloc] init];
    vc.careCase = careCase;
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 15;
}

@end
